package com.wyb.moto.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wyb.moto.common.exception.AppException;
import com.wyb.moto.common.exception.Result;
import com.wyb.moto.dao.LoginMapper;
import com.wyb.moto.model.Leave;
import com.wyb.moto.model.User;
import com.wyb.moto.model.YouEr;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

@Transactional
@Controller
@RequestMapping("/logincontroller")
public class LoginController {
	@Autowired
    private HttpServletRequest request; //自动注入request
	@Autowired
	private LoginMapper loginMapper;
	
	@RequestMapping("/login")
	@ResponseBody
	public Result login(){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String type = request.getParameter("type");
		User user;
		if(StrUtil.isBlank(username)){
			throw new AppException("用户名为空！");
		}
		//幼儿
		if(type.equals("1")){
			user =this.loginMapper.getYeByUsername(username);
		//admin
		}else{
			user = this.loginMapper.getadminByUsername(username);
		}
		if(user == null){
			throw new AppException("不存在该用户");
		}
		String passwordDb = user.getPassword();
		if(!password.equals(passwordDb)){
			throw new AppException("密码错误!");
		}
		request.getSession().setAttribute("user", user);
		return Result.ok(user);
	}
	@RequestMapping("/saveQj")
	@ResponseBody
	public Result saveQj(){
		String kssj = request.getParameter("kssj");
		String jssj = request.getParameter("jssj");
		String qjsy = request.getParameter("qjsy");
		String yeid = ((YouEr)request.getSession().getAttribute("user")).getId();
		String id = RandomUtil.simpleUUID();
		this.loginMapper.saveLeave(kssj, jssj, qjsy, yeid, id);
		return Result.ok(null);
		
	}
	@RequestMapping("/zhuce")
	@ResponseBody
	public Result zhuce(){
		String username = request.getParameter("username");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String xb = request.getParameter("xb");
		String sr = request.getParameter("sr");
		String bh = request.getParameter("bh");
		String bjbh = request.getParameter("bjbh");
		String id = RandomUtil.simpleUUID();
		
		this.loginMapper.saveZhuce(name,id, bh, password, xb, sr, username,bjbh);
		return Result.ok(null);
		
	}
	@RequestMapping("/queryQj")
	@ResponseBody
	public Map<String,Object> queryQj(){
		String ymString =request.getParameter("ym");
		if(StrUtil.isBlank(ymString)){
			ymString= "0";
		}
		int ym = Integer.valueOf(ymString);
		
		String searchtext = request.getParameter("searchtext");
		if(StrUtil.isAllBlank(searchtext)){
			searchtext = "%";
		}
		List<Leave> list = this.loginMapper.queryQj(searchtext,ym);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data",list );
		map.put("recordsTotal",list.size() );
		map.put("recordsFiltered", list.size());
		
		return map;
		
	}
	
	@RequestMapping("/deleteQj")
	@ResponseBody
	public Result deleteQj(){
		String id = request.getParameter("id");
		this.loginMapper.deleteQj(id);
		return Result.ok(null);
		
	}
}
